﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TreatmentAddViewModel.cs" company="ELEN 7045 GROUP II">>
//   
// </copyright>
// <summary>
//   The Studies View Model.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Presentation.Module.StudyDefinition.ViewModels
{
    #region

    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows.Input;

    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Prism.Regions;

    using Presentation.Module.Setup.Interfaces.Services;
    using Presentation.Module.Setup.Models;
    using Presentation.Module.StudyDefinition.Interfaces.Services;
    using Presentation.Module.StudyDefinition.Interfaces.ViewModels;
    using Presentation.Module.StudyDefinition.Interfaces.Views;
    using Presentation.Module.StudyDefinition.Models;

    #endregion

    /// <summary>
    /// TreatmentAddViewModel Implementation
    /// </summary>
    public class TreatmentAddViewModel : StudyDefinitionViewModelBase, ITreatmentAddViewModel, INotifyPropertyChanged
    {
        #region Constants and Fields

        /// <summary>
        /// The skills service instance.
        /// </summary>
        private readonly ISkillsService skillsServiceInstance;

        /// <summary>
        /// The available tasks.
        /// </summary>
        private ObservableCollection<Task> availableTasks;

        /// <summary>
        /// A treatment item
        /// </summary>
        private Treatment currentItem;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="TreatmentAddViewModel"/> class.
        /// </summary>
        /// <param name="regionManager">
        /// The region manager.
        /// </param>
        /// <param name="studiesService">
        /// The studies service.
        /// </param>
        /// <param name="skillsService">
        /// The skills Service.
        /// </param>
        public TreatmentAddViewModel(IRegionManager regionManager, IStudiesUIProxyService studiesService, ISkillsService skillsService)
            : base(regionManager, studiesService)
        {

            // Here I'm reusing an existing service from the setup sub domain
            this.skillsServiceInstance = skillsService;

            // Setting up command delegates 
            this.AddTreatment = new DelegateCommand(this.OnAddTreatment);
            
            this.AddTaskToTreatment = new DelegateCommand<Task>(this.OnAddTaskToTreatment);
            this.RemoveTaskFromTreatment = new DelegateCommand<PlannedTask>(this.OnRemoveTaskFromTreatment);
        }

        #endregion

        #region Events

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets AddTaskToTreatment.
        /// </summary>
        public ICommand AddTaskToTreatment { get; set; }

        /// <summary>
        /// Gets the add treatment command.
        /// </summary>
        public ICommand AddTreatment { get; set; }

        /// <summary>
        /// Gets or sets AvailableTasks.
        /// </summary>
        public ObservableCollection<Task> AvailableTasks
        {
            get
            {
                this.availableTasks = null;
                
                this.availableTasks = new ObservableCollection<Task>();
                var skills=this.skillsServiceInstance.GetAllSkills();

                foreach (var skill in skills)
                {
                    var tasks = this.skillsServiceInstance.GetAllTasksForSkill(skill);

                    foreach (var task in tasks)
                    {
                        this.availableTasks.Add(task);    
                    }
                }
                
                return availableTasks;
            }

            set
            {
                availableTasks = value;
            }
        }

        /// <summary>
        /// Gets or sets CurrentItem.
        /// </summary>
        public Treatment CurrentItem
        {
            get
            {
                return this.currentItem;
            }

            set
            {
                if (value == null)
                {
                    return;
                }

                this.currentItem = value;
                this.RaiseOnPropertyChanged("CurrentItem");
            }
        }

        /// <summary>
        /// Gets or sets RemoveTaskFromTreatment.
        /// </summary>
        public ICommand RemoveTaskFromTreatment { get; set; }

        /// <summary>
        /// Gets or sets the view.
        /// </summary>
        /// <value>
        /// The treatment add view.
        /// </value>
        public ITreatmentAddView View { get; set; }

        #endregion

        #region Implemented Interfaces

        #region ITreatmentAddViewModel

        /// <summary>
        /// The initialize.
        /// </summary>
        public void Initialize()
        {
            // Populate the current item to for use by the view.
            this.currentItem = new Treatment();
            this.CurrentItem.PlannedTasks = new ObservableCollection<PlannedTask>();

            

            // this.CreateMockTasks();
        }

        /// <summary>
        /// The on add task to treatment.
        /// </summary>
        /// <param name="task">
        /// The task.
        /// </param>
        public void OnAddTaskToTreatment(Task task)
        {
            if (task == null)
            {
                return;
            }

            var plannedTask = new PlannedTask();

            plannedTask.Name = task.Name;
            plannedTask.Description = task.Description;

            this.CurrentItem.PlannedTasks.Add(plannedTask);

            this.RaiseOnPropertyChanged("CurrentItem");


            // this.AvailableTasks.Remove(plannedTask);
            this.RaiseOnPropertyChanged("AvailableTasks");
        }

        /// <summary>
        /// Called when [add treatment].
        /// </summary>
        public void OnAddTreatment()
        {
            this.StudiesServiceInstance.AddTreatment(this.currentItem);

            this.CurrentItem = new Treatment();
            this.RaiseOnPropertyChanged("CurrentItem");
        }

        /// <summary>
        /// The on remove task from treatment.
        /// </summary>
        /// <param name="plannedTask">
        /// The planned task.
        /// </param>
        public void OnRemoveTaskFromTreatment(PlannedTask plannedTask)
        {
            if (plannedTask == null)
            {
                return;
            }

            // this.AvailableTasks.Add(plannedTask);
            this.RaiseOnPropertyChanged("AvailableTasks");


            this.CurrentItem.PlannedTasks.Remove(plannedTask);
            this.RaiseOnPropertyChanged("CurrentItem");
        }

        #endregion

        #endregion

        #region Methods

        /// <summary>
        /// Raises the on property changed.
        /// </summary>
        /// <param name="propertyName">
        /// Name of the property.
        /// </param>
        protected void RaiseOnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// Called when [cancel].
        /// </summary>
        private void OnCancel()
        {
            this.RegionManagerInstance.Regions["MainRegion"].Deactivate(this.View);
        }

        #endregion
    }
}
